/*    */ package com.zimbra.cs.db;
/*    */ 
/*    */ import com.zimbra.common.util.Log;
/*    */ import com.zimbra.common.util.ZimbraLog;
/*    */ import java.sql.SQLException;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public abstract class AbstractRetry<T>
/*    */ {
/*    */   private static final int RETRY_LIMIT = 10;
/*    */   private static final int RETRY_DELAY = 1000;
/*    */   
/*    */   public abstract ExecuteResult<T> execute()
/*    */     throws SQLException;
/*    */   
/*    */   protected boolean retryException(SQLException sqle)
/*    */   {
/* 35 */     return (Db.errorMatches(sqle, Db.Error.BUSY)) || (Db.errorMatches(sqle, Db.Error.LOCKED));
/*    */   }
/*    */   
/*    */ 
/*    */ 
/*    */ 
/* 41 */   private static int retryCount = 0;
/*    */   
/* 43 */   public static int getTotalRetries() { return retryCount; }
/*    */   
/*    */   private synchronized void incrementTotalRetries()
/*    */   {
/* 47 */     retryCount += 1;
/*    */   }
/*    */   
/*    */ 
/*    */ 
/*    */   public ExecuteResult<T> doRetry()
/*    */     throws SQLException
/*    */   {
/* 55 */     int tries = 0;
/* 56 */     SQLException sqle = null;
/* 57 */     while (tries < 10) {
/*    */       try {
/* 59 */         return execute();
/*    */       } catch (SQLException e) {
/* 61 */         if (retryException(e)) {
/* 62 */           sqle = e;
/* 63 */           tries++;
/* 64 */           ZimbraLog.dbconn.warn("retrying connection attempt:" + tries + " due to possibly recoverable exception: ", e);
/* 65 */           incrementTotalRetries();
/*    */           try {
/* 67 */             Thread.sleep(1000L);
/*    */           }
/*    */           catch (InterruptedException ie) {}
/*    */         } else {
/* 71 */           throw e;
/*    */         }
/*    */       }
/*    */     }
/* 75 */     throw new SQLException("DB retry gave up after " + tries + " attempts.", sqle);
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/AbstractRetry.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */